package com.lb.shopguide.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.baselib.base.DialogBase;
import com.lb.shopguide.R;

/**
 * Created by kang on 2017/9/8.
 */

public class DialogSetPointConfig extends DialogBase implements View.OnClickListener {

    private EditText etPointConfig;
    private ImageView ivClose;
    private TextView tvSure;

    public static DialogSetPointConfig getDialogSetPointConfig() {
        DialogSetPointConfig dialogSetSuccess = new DialogSetPointConfig();
        return dialogSetSuccess;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View mFragmentView = inflater.inflate(R.layout.dialog_set_point_config, container, false);
        initData();
        initView(mFragmentView);

//        setDialogPosition();
        bindData();
        return mFragmentView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT);
            dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                KeyboardUtils.showSoftInput(etPointConfig);
            }
        }, 500);
    }

    private void initData() {

    }

    private void initView(View view) {
        ivClose = view.findViewById(R.id.iv_close);
        tvSure = view.findViewById(R.id.tv_sure);
        etPointConfig = view.findViewById(R.id.et_point_config);

        initListener();

    }

    private void bindData() {

    }

    private void initListener() {
        ivClose.setOnClickListener(this);
        tvSure.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                DialogSetPointConfig.this.dismiss();
                break;
            case R.id.tv_sure:
                String point = etPointConfig.getText().toString();
                if (TextUtils.isEmpty(point)) {
                    ToastUtils.showShort("请先输入金额");
                    break;
                }
                float fPoint = Float.parseFloat(point);
                if (fPoint == 0f) {
                    ToastUtils.showShort("请输入正确的金额");
                    break;
                }
                mOnSureClickListener.onSure(Integer.parseInt(point));
                break;
        }

    }

    public interface OnSureClickListener {

        void onSure(int point);
    }

    private OnSureClickListener mOnSureClickListener;

    public void setOnSureClickListener(OnSureClickListener listener) {
        mOnSureClickListener = listener;
    }
}
